package com.trgis.yw.word.service.impl;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.trgis.yw.db.AccessODBCUtils;
import com.trgis.yw.db.AccessUtils;
import com.trgis.yw.word.service.WordService;
import java.util.logging.Logger;
import org.apache.commons.lang.StringUtils;

public class WordServiceImpl implements WordService {

    public static final String TABLENAME = " ZD_GYZSYQDJ";
    public static final String SEARCH_COLUMN = " YBDJH";
    public static final String RESULT_COLUMN = "JZFW";
    private AccessUtils db;

    public WordServiceImpl(String mdb) {
        db = new AccessODBCUtils(mdb);
    }

    @Override
    public String getZDSZ(String id) {
        String sql = "select " + RESULT_COLUMN + " from " + TABLENAME
                + " where " + SEARCH_COLUMN + " = " + "'" + id + "'";
        ResultSet resultSet = null;
        try {
            resultSet = db.executeQuery(sql);
            resultSet.next();
            return resultSet.getString(1);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return "";
    }

    @Override
    public void updateData(String string, String id) {
        if ("".equals(string)) {
            return;
        }
        String sql = "update " + TABLENAME + " set HCQKSM = '" + string
                + "' where " + SEARCH_COLUMN + " = " + "'" + id + "'";
        System.out.println(sql);
        try {
            db.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    @Override
    public String getQKSM(String id) {
        String sql = "select HCQKSM from " + TABLENAME + " where " + SEARCH_COLUMN + " = " + "'" + id + "'";
        ResultSet resultSet = null;
        try {
            resultSet = db.executeQuery(sql);
            resultSet.next();
            String result = resultSet.getString(1);
            System.out.println(id + ": " + result);
            if(StringUtils.isEmpty(result) || result == null) {
                return "";
            }
            return result;
        } catch (SQLException e) {
            return "";
        }
        
    }

    public void destory() {
        try {
            db.destory();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) throws SQLException {
        AccessUtils db = new AccessODBCUtils("wz");
        String id = "22-100-216001-0008-000";
        String sql = "select " + RESULT_COLUMN + " from " + TABLENAME + " where "
                + SEARCH_COLUMN + " = " + "'" + id + "'";
        System.out.println(sql);
        ResultSet set = db.executeQuery(sql);
        while (set.next()) {
            System.out.println(set.getString(1));
        }
    }
}
